<?php

namespace plugin\webman\xingcunzhe;
use plugin\webman\xingcunzhe\Common;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;

class Taosha
{

    // 定时器发送状态
    public static function timer()
    { 
            // $key = 'xcz_cutdown';
            // $timer = Redis::get($key);  //倒计时
            // // 距离本期结束还有多少秒
            // $nextStartTime = Common::getnextexpecttime();
            // if(!$timer && $timer <= 0){
            //     $timer = Redis::set($key,120);  //每期120s
                
            // }else{
            //      Redis::set($key,$timer-1);
            // }

            $timer = Common::getnextexpecttime();

            $data=[
                'code'=>1,
                'msg'=>'本局剩余时间',
                'info'=>(int)$timer
            ];
            Gateway::sendToAll(Common::returndata('timer',$data));
            // var_dump($timer);
            switch ($timer) {
                case '118':
                    self::game_start();
                    break;
                case '90':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;
                    case '87':
                        self::robotbet();
                        break;
                    
                case '81':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;  
                    case '78':
                        self::robotbet();
                        break;
               
                case '72':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;
                    case '70':
                        self::robotbet();
                        break;
                 
                case '63':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;  
                    case '61':
                        self::robotbet();
                        break;
                  
                case '54':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;
                    case '52':
                        self::robotbet();
                        break;
                   
                case '45':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;  
                    case '43':
                        self::robotbet();
                        break;
                   
                case '36':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;
                    case '34':
                        self::robotbet();
                        break;
 
                case '27':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;   
                    case '25':
                        self::robotbet();
                        break;
                   
                case '18':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;  
                    case '16':
                        self::robotbet();
                        break;
                    
                case '9':
                    self::floorchange(2);
                    self::floorchange(3);
                    self::floorchange(4);
                    break;
      
                
            }
        
    }  

    // 广播  游戏开始 创建期号
    public static function game_start(){
        $nowexpect = Common::getnowexpect();
        
        $createexpect = Common::createnewexpect($nowexpect);
        Db::table("wa_xcz_position")->where("id","<","9999999")->update(['position'=>0,'gezi'=>0]);
        // 当前期号的层级改为1层

        $info['expect']= Db::table("wa_xcz_expect")->where("expect",$nowexpect)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        $key = 'xcz_cutdown';
        $timer = Redis::get($key);  //倒计时

        if($timer<120){
            $broadcast = [            
                'code'=>1,
                'msg'=>'新的一期开始',
                'info'=>$info
            ];
            
            Gateway::sendToAll(Common::returndata('gamestart',$broadcast));
        }
        $rooms = [2,3,4];
        foreach ($rooms as $key =>$value){
            $louceng["upexpect"] = Common::getexpectinfo($value,$nowexpect);
            $louceng['list']=Common::getuserlist($value,0);
            // 广播_楼层人数变化
            $broadcast = [            
                'code'=>1,
                'msg'=>'楼层人数变化',
                'info'=>$louceng
            ];
            
            Gateway::sendToAll(Common::returndata('upusernums',$broadcast));
           
        }

    }

    // 广播 楼层变化  传值房间号
    public static function floorchange($roomid){
        $nowexpect = Common::getnowexpect();

        $info = Db::table("wa_xcz_expect")->where(["expect"=>$nowexpect,"roomid"=>$roomid])->first();
        if(!$info){
            $createexpect = Common::createnewexpect($nowexpect);
            $info = Db::table("wa_xcz_expect")->where(["expect"=>$nowexpect,"roomid"=>$roomid])->first();
        }
        switch ($roomid) {
            // case '2':
            //     if($info->nowlevel<10){
            //         $newlevel = $info->nowlevel+1;
            //         $oldlevel= $info->nowlevel;
            //     }else{
            //         $newlevel=1;
            //         $oldlevel= 10;
            //     }
            //     $maxgezi = 5;
            //     break;
            // case '3':
            //     if($info->nowlevel<7){
            //         $newlevel = $info->nowlevel+1;
            //         $oldlevel= $info->nowlevel;
            //     }else{
            //         $newlevel=1;
            //         $oldlevel= 7;
            //     }
            //     $maxgezi = 4;
            //     break;
            // case '4':
            //     if($info->nowlevel<4){
            //         $newlevel = $info->nowlevel+1;
            //         $oldlevel= $info->nowlevel;
            //     }else{
            //         $newlevel=1;
            //         $oldlevel= 4;
            //     }
            //     $maxgezi = 3;
            //     break;           
            case '2':
                if($info->nowlevel<10){
                    $newlevel = $info->nowlevel+1;
                    $oldlevel= $info->nowlevel;
                }else{
                    $newlevel=1;
                    $oldlevel= 10;
                }
                $maxgezi = 4;
                break;
            case '3':
                if($info->nowlevel<10){
                    $newlevel = $info->nowlevel+1;
                    $oldlevel= $info->nowlevel;
                }else{
                    $newlevel=1;
                    $oldlevel= 10;
                }
                $maxgezi = 6;
                break;
            case '4':
                if($info->nowlevel<10){
                    $newlevel = $info->nowlevel+1;
                    $oldlevel= $info->nowlevel;
                }else{
                    $newlevel=1;
                    $oldlevel= 10;
                }
                $maxgezi = 2;
                break;               
            
        }

        Db::table("wa_xcz_expect")->where(["expect"=>$nowexpect,"roomid"=>$roomid])->update(['nowlevel'=>$newlevel]);
            
        

        $b= Db::table("wa_xcz_expect")->where("expect",$nowexpect)->where("roomid",$roomid)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        
        $res['expect'] = $b[0];
        $res['roomid']=$roomid;
        $res['killedgezi']=rand(0,$maxgezi);  //被杀的格子是哪个
// 开奖 分配中奖金额
        Common::kaijiang($nowexpect,$roomid,$oldlevel,$res['killedgezi']);
        $res['list']=Common::getuserlist($roomid,$newlevel);


        
        // Common::kaijiang("202401270537",$roomid,$oldlevel,$res['killedgezi']);
        $broadcast = [            
            'code'=>1,
            'msg'=>'楼层变化',
            'info'=>$res
        ];
        
        Gateway::sendToAll(Common::returndata('levelup',$broadcast));

        $louceng["upexpect"] = Common::getexpectinfo($roomid,$nowexpect);
        $louceng['list']=$res['list'];
        // 广播_楼层人数变化
        sleep(2);
        $broadcast = [            
            'code'=>1,
            'msg'=>'楼层人数变化',
            'info'=>$louceng
        ];
        
        Gateway::sendToAll(Common::returndata('upusernums',$broadcast));


    }
    // 房间楼层开奖
    public static function levelkaijiang($roomid){

    }

    // 广播 
    public static function testkaijiang(){
        Common::kaijiang("202401221263",2,1,5);
    }

    // 获取个人投注信息
    public static function getmytz($client_id,$data){
        $expect = Common::getnowexpect();
        // var_dump($expect);
        // $expect = "202401221263";

        $touzhu = Db::table("wa_xcz_touzhu")->where(['uid'=>$data['uid'],'expect'=>$expect,'roomid'=>$data['roomid'],'level'=>$data['level']])->limit(20)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if($touzhu){
            $i = 0;
            foreach($touzhu as $k => $v){
                $i = $i+1;
                
                if($v['status']==1){
                    $rs['result']="恭喜您，躲避成功，获取奖金".$v['rewards'];
                    $rs['status']=1;
                }else{
                    $rs['result']="躲避失败，损失".$v['coin'];
                    $rs['status']=2;
                }
                $rs['expect']=$expect;
                $a[]= $rs;
            }
            return $a[$i-1];
        }else{
            $rs['expect']=$expect;
            $a['result']="未参与本期躲避";
            $a['status']=0;
            return $a;
        }
    }
    // 领取奖励
    public static function lingqu($client_id,$data){
        $list = Db::table("wa_touzhu")->where(['uid'=>$data['uid'],'expect'=>$data['expect']])->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        $jiangli = 0;
        foreach($list as $k => $v){
            // var_dump($v);
            if($v['status']==1 && $v['iflingqu']==0){
                // var_dump($v);die;
                $jiangli = $v['rewards']+$v['coin'];
               Common::updateusercoin($v['uid'],$jiangli,1);
               Db::table("wa_touzhu")->where("id",$v['id'])->update(['iflingqu'=>1]);
                // 记录金币消费
                $record = [
                    'uid'=>$data['uid'],
                    'coin'=>$v['rewards'],
                    'type'=>0,
                    'remark'=>'期号:'.$v['expect'].'在房间'.$v['roomid'].'躲避成功奖励发放并返回投注金额'.$v['coin'],
                    'addtime'=>date("Y/m/d H:i:s",time())
                 ];
                Common::usercoin_record($record);

                // 写入排行榜记录
                $ifexit = Db::table("wa_paihang")->where("uid",$v['uid'])->where("roomid",$v['roomid'])->get()->map(function ($value) {return (array)$value;})
                ->toArray();
                // var_dump($ifexit);die;
                if($ifexit){
                    $reward = $v['rewards']+$ifexit[0]['rewards'];
                    $bangdan = [
                        'rewards'=>$reward,
                    ];
                    // var_dump($bangdan);die;
                    Db::table("wa_paihang")->where("uid",$v['uid'])->update(['rewards'=>$reward]);
                }else{
                    $bangdan = [
                        'uid'=>$v['uid'],
                        'rewards'=>$v['rewards'],
                        'roomid'=>$v['roomid'],
                    ];
                    // var_dump($bangdan);die;
                    Db::table("wa_paihang")->insert($bangdan);
                }
                

            } 
        }
        $userinfo = Common::getuserinfo($data['uid']);
        $res=[
            'code'=>1,
            'msg'=>'领取成功',
            'info'=>$userinfo
        ];    
        return $res;
    }

    // 排行帮
    public static function paihang($client_id,$data){
        $paihang = Db::table("wa_paihang")->orderByDesc('rewards',true)->get()->map(function ($value) {return (array)$value;})
        ->toArray();

        foreach($paihang as $k =>$v){
            $v['userinfo']=Common::getuserinfo($v['uid']);
            $list[]=$v;
        }
        $info['list']=$list;
        $res=[
            'code'=>1,
            'msg'=>'成功',
            'info'=>$info
        ];    
        return $res;
    }

    // 检查url
    public static function checkurl($client_id,$data){
        $datas = explode('&',$data['data']['data']);
        foreach($datas as $k =>$v){
            $chai = explode('=',$v);
            
            $a[0]=$chai[0];
            $a[1]=$chai[1];
            $new[]=$a;
        }
        // var_dump($new);die;
        foreach($new as $kt =>$vt){
            $b[$vt[0]]=$vt[1];
            $new2=$b;  //所有前端给的参数都在这里接受.  传的啥可以直接取出来
        }

        $checktoken =   Common::checktoken2($client_id,$new2['uid'],$new2['token']);
        if($checktoken!=1){
            Gateway::sendToClient($client_id,Common::returndata('tokenexpire',$data));
            exit();
        }
        // $user['userinfo']['token'] = User::updatetoken2($new2['uid']);
        $user['userinfo'] = User::getuserinfobyid($new2['uid']); 
        // $user['userinfo'] = Common::getuserinfo($new2['uid']); 
        // var_dump($user['userinfo']);die;
        $rs['code']=1;
        $rs['msg']='登录成功';
        $rs['info']=$user;
        return $rs;
    }


 

    // 切换小房间
    public static function changesmallroom($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);
        if($checktoken==1){
            $nowexpect = Common::getnowexpect();
            $upuser = Common::upuserpostion($data['uid'],$data['roomid'],$data['level'],$data['gezi']);
            //更新期号人数信息
            $louceng["upexpect"] = Common::upexpect($data['roomid'],$nowexpect,$data['gezi']);
            $louceng['list']=Common::getuserlist($data['roomid'],$data['level']);
            // 广播_楼层人数变化
            $broadcast = [            
                'code'=>1,
                'msg'=>'楼层人数变化',
                'info'=>$louceng
            ];
            
            Gateway::sendToAll(Common::returndata('upusernums',$broadcast));
            $userinfo = Common::getuserinfo($data['uid']);
            $userinfo['allbets']=Common::getuserallbet($nowexpect,$data['roomid']);
            $data=[
                'code'=>1,
                'msg'=>'切换成功',
                'info'=>$userinfo
            ];
            return $data;

        }
    }


     





    // 用户进入房间

    public static function  enterroom($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']); 
        $nowexpect= Common::getnowexpect();
        Common::createnewexpect($nowexpect);
        if($checktoken==1){
            $roominfo =Db::table("wa_rooms")->where(["status"=>1,'id'=>$data['roomid']])->first();
            // var_dump($data['roomid']);die;
            if(!$roominfo){
                $data=[
                    'code'=>1051,
                    'msg'=>'房间不存在',
                    'info'=>[]
                ];
                return $data;
            }

            // 更新用户所在房间
            $changeroom = Common::changeuserroomid($data['uid'],$data['roomid']);

            $userinfo = Common::getuserinfo($data['uid']);
            
            // var_dump($userinfo['coin'],$roominfo->minprice);die;
            if($userinfo['coin']<$roominfo->minprice){
                $data=[
                    'code'=>1052,
                    'msg'=>'最低进入房间限制为'.$roominfo->minprice.'金币',
                    'info'=>[]
                ];
                return $data;
            }



            $nowexpect = Common::getnowexpect();
            
            $expectinfo = Db::table("wa_xcz_expect")->where(["expect"=>$nowexpect,"roomid"=>$data['roomid']])->first();
            // var_dump($expectinfo);
            if(!$expectinfo){
                // 生成本期号
                $expectinfo = Common::createexpect($client_id,$data['roomid'],$nowexpect);
                $expectinfo['numlist']=explode(',',$expectinfo['numlist']);
                $expectinfo['roomid']=(int)$expectinfo['roomid'];
                $info['list']=Common::getuserlist($data['roomid'],$expectinfo['nowlevel']);

                 // 重新核算每个格子的人数
                 switch ($expectinfo['roomid']) {
                    case '2':
                        $i = [0,1,2,3,4];
                        break;
                    case '3':
                        $i = [0,1,2,3];
                        break;
                    case '4':
                        $i = [0,1,2];
                        break;            
                }
                foreach ($i as $key => $value) {
                    $expectinfo['numlist'] = Db::table("wa_xcz_position")->where("roomid",$expectinfo['roomid'])->where("position",$expectinfo['nowlevel'])->where("gezi",$key)->count();
                }

            }else{
                $expectinfo->numlist=explode(',',$expectinfo->numlist);
                $expectinfo->roomid=(int)$expectinfo->roomid;
                $info['list']=Common::getuserlist($data['roomid'],$expectinfo->nowlevel);

                 // 重新核算每个格子的人数
                switch ($expectinfo->roomid) {
                    case '2':
                        $i = [0,1,2,3,4];
                        break;
                    case '3':
                        $i = [0,1,2,3];
                        break;
                    case '4':
                        $i = [0,1,2];
                        break;            
                }
                foreach ($i as $key => $value) {
                    $expectinfo->numlist[$key] = (string)Db::table("wa_xcz_position")->where("roomid",$expectinfo->roomid)->where("position",$expectinfo->nowlevel)->where("gezi",$key)->count();
                }
            }


           


            // 本期信息
           

            // 每层的赔率信息

            $info['peilvlist'] = Common::geipeilvlist($data['roomid']);
            $info['userinfo']=$userinfo;
            $info['userinfo']['allbets']=Common::getuserallbet($nowexpect,$data['roomid']);
            $info['expectinfo']=$expectinfo;
            // var_dump($info['expectinfo']);
            $louceng["upexpect"] = $expectinfo;
            $louceng['list']=$info['list'];
            // 广播_楼层人数变化
            $broadcast = [            
                'code'=>1,
                'msg'=>'楼层人数变化',
                'info'=>$louceng
            ];
            
            Gateway::sendToAll(Common::returndata('upusernums',$broadcast));


            $info['playerlist']=Common::getplayerlist($data['roomid']);
            $res=[
                'code'=>1,
                'msg'=>'进入成功',
                'info'=>$info
            ];    
            return $res;
        }
    }



       // 机器人投注
    public static function robotbet(){
        // $array = Db::table('wa_users')->where("isrobot",1)->where("roomid",">",1)->get()->map(function ($value) {return (array)$value;})
        // ->toArray();
        $array = Db::table('wa_users')->where("isrobot",1)->where("roomid",2)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        $numToPick = rand(1, count($array)); // 需要抽取的随机数量
        $numToPick = 1; 
        $nowexpect = Common::getnowexpect();
        
        if($numToPick>0){
            // 创建一个空数组存放抽取到的元素
            $pickedElements = [];
            for ($i = 0; $i < $numToPick; $i++) {
                $index = array_rand($array); // 生成随机索引值
                $element = $array[$index]; // 根据索引值获取元素
                
                unset($array[$index]); // 将已经选取的元素移除原始数组，确保不会重复选取相同的元素
                
                $pickedElements[] = $element; // 添加到结果数组中
            }
            
            // print_r($pickedElements); // 输出抽取到的元素
            
            foreach ($pickedElements as $k => $v) {
                // 期号信息  当前 第几层  
                $expectinfo = Db::table("wa_xcz_expect")->where(["expect"=>$nowexpect,"roomid"=>$v['roomid']])->get()->map(function ($value) {return (array)$value;})
                ->toArray();
     
                if(!$expectinfo){
                    // 生成本期号
                    $expectinfo = Common::createexpect($v['id'],$v['roomid'],$nowexpect);
                    
                }else{
                    $expectinfo = $expectinfo[0];
                }

                // 最大格子
                switch ($v['roomid']) {
                    case '2':
                       $max = 4;
                        break;
                        case '3':
                            $max = 3;
                            break;
                            case '4':
                                $max = 2;
                                break;                   

                }
                $randgezi = rand(0,$max);
                $config =  Common::getbaseconfig();
                // var_dump($config);die;
                $coin = rand($config['logo']['robotmin'],$config['logo']['robotmax']);
                // 记录投注
                $record = [
                    'uid'=>$v['id'],
                    'roomid'=>$v['roomid'],
                    'expect'=>$nowexpect,
                    'level'=>$expectinfo['nowlevel'],
                    'gezi'=>$randgezi,
                    'coin'=>$coin,
                    'isrobot'=>0,
                    'result'=>'',
                    'ifwin'=>2
                ];
                $in =  Db::table("wa_xcz_touzhu")->insertGetId($record);

                // 记录房间号
                $roomsinfo = Db::table("wa_xcz_position")->where(['roomid'=>$v['roomid'],'uid'=>$v['id']])->first();
                if($roomsinfo){
                    Db::table("wa_xcz_position")->where(['roomid'=>$v['roomid'],'uid'=>$v['id']])->update(['position'=>$record['level'],'gezi'=>$record['gezi']]);
                }else{
                    Db::table("wa_xcz_position")->insert(['uid'=>$v['id'],'roomid'=>$v['roomid'],'position'=>$record['level'],'gezi'=>$record['gezi']]);
                }

                $louceng["upexpect"] = Common::upexpect($v['roomid'],$nowexpect,$randgezi);
                $louceng['list']=Common::getuserlist($v['roomid'],$record['level']);
                // 广播_楼层人数变化
                $broadcast = [            
                    'code'=>1,
                    'msg'=>'楼层人数变化',
                    'info'=>$louceng
                ];
                
                Gateway::sendToAll(Common::returndata('upusernums',$broadcast));



            }

        }
        
                
    }


    // 投注 
    public static function touzhu($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);
        if($checktoken==1){
            $nowexpect = Common::getnowexpect();
            // 期号信息  当前 第几层  
            $expectinfo = Db::table("wa_xcz_expect")->where(["expect"=>$nowexpect,"roomid"=>$data['roomid']])->first();
            if(!$expectinfo){
                // 生成本期号
                $expectinfo = Common::createexpect($client_id,$data['roomid'],$nowexpect);
                $expectinfo['numlist']=explode(',',$expectinfo['numlist']);
            }else{
          
                $numlist = $expectinfo->numlist;
             
                $expectinfo->numlist=explode(',',$numlist);
            }

            // 判断用户余额
            $userinfo = Common::getuserinfo($data['uid']);
            $userinfo['allbets']=Common::getuserallbet($nowexpect,$data['roomid']);
            if($userinfo['coin']<$data['coin']){
                $res=[
                    'code'=>1001,
                    'msg'=>'宝石不足',
                    'info'=>$userinfo
                ];    
                return $res;
            }
  
            // 扣除金币
            $couchu = Common::updateusercoin($data['uid'],$data['coin'],0,"幸存者投注");
            
            if($couchu!=1){
                $tz=[
                    'code'=>1085,
                    'msg'=>"投入失败",
                    'info'=>$userinfo
                ];
                return $tz;
            }else{
                // 记录金币消费
                $record = [
                    'uid'=>$data['uid'],
                    'coin'=>$data['coin'],
                    'type'=>0,
                    'remark'=>'幸存者房间'.$data['roomid'].'号投注'.$data['coin'].'金币',
                    'addtime'=>date("Y/m/d H:i:s",time())
                ];
                Common::usercoin_record($record);

                // 记录投注
                $record = [
                    'uid'=>$data['uid'],
                    'roomid'=>$data['roomid'],
                    'expect'=>$nowexpect,
                    'level'=>$data['level'],
                    'gezi'=>$data['gezi'],
                    'coin'=>$data['coin'],
                    'isrobot'=>0,
                    'result'=>'',
                    'ifwin'=>2,
                    'addtime'=>date("Y/m/d H:i:s",time())
                ];
               $in =  Db::table("wa_xcz_touzhu")->insertGetId($record);

                 // 更新用户位置信息
                 $upuser = Common::upuserpostion($data['uid'],$data['roomid'],$data['level'],$data['gezi']);

                //更新期号人数信息
                $louceng["upexpect"] = Common::upexpect($data['roomid'],$nowexpect,$data['gezi']);
                $louceng['list']=Common::getuserlist($data['roomid'],$data['level']);
                // 广播_楼层人数变化
                $broadcast = [            
                    'code'=>1,
                    'msg'=>'楼层人数变化',
                    'info'=>$louceng
                ];
                
                Gateway::sendToAll(Common::returndata('upusernums',$broadcast));

                $userinfo = Common::getuserinfo($data['uid']);
                $userinfo['allbets']=Common::getuserallbet($nowexpect,$data['roomid']);

                $tz=[
                    'code'=>1,
                    'msg'=>"投入成功",
                    'info'=>$userinfo
                ];
                return $tz;

            }




        }
    }




   
    // 离开房间
    public static function leaveroom($client_id,$data){
        $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);

        if($checktoken==1){
            $roominfo =Db::table("wa_rooms")->where(["status"=>1,'id'=>$data['roomid']])->first();
            // var_dump($data['roomid']);die;
            if(!$roominfo){
                $data=[
                    'code'=>1051,
                    'msg'=>'房间不存在',
                    'info'=>[]
                ];
                return $data;
            }
            $userinfo = Common::getuserinfo($data['uid']);
            Common::leaveroom($client_id,$data["roomid"],$userinfo);
            $leaveuser=[
                'uid'=>$data['uid'],
            ];
            $data=[
                'code'=>1,
                'msg'=>$userinfo['username'].'离开房间',
                'info'=>$leaveuser
            ];
            return $data;
            
        }
    }

    // onCloseleaveroom  关闭连接 退出房间
    public static function onCloseleaveroom($client_id,$uid){
        // $uid = Gateway::getUidByClientId($client_id);
        // var_dump($uid);die;
        // $data['uid']=$uid;
        // $user=Db::table("wa_users")->where("id",$uid)->first();
        // $data['roomid']=$user->roomid;
        // $data['token']=$user->token;
        // return self::leaveroom($client_id,$data);
    }




    // 获取投注记录
    public static function getrecord($client_id,$data){    
        $list = Db::table("wa_xcz_touzhu")->where("uid",$data['uid'])->orderByDesc('id',true)->limit(20)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if($list){
            foreach ($list as $k => $v) {
                switch ($v['roomid']) {
                    case '2':
                        $a['name']='简单';
                        break;
                    case '3':
                        $a['name']='中级';
                        break;  
                    case '4':
                        $a['name']='高级';
                        break;              
    
                }
                $a['level']=$v['level'];
                $a['coin']=$v['coin'];
                $a['status']=$v['ifwin'];
                $a['addtime']=$v['addtime'];
                if($v['rewards']==NULL){
                    $a['rewards']="0";
                }else{
                    $a['rewards']=$v['rewards'];
                }
                $new[]=$a;
    
            }
        }else{
            $new=[];
        }

            $data=[
                'code'=>1,
                'msg'=>'success',
                'info'=>$new
            ];
            return $data;


    }   
    // public static function getrecord($client_id,$data){
    //     $checktoken =   Common::checktoken($client_id,$data['uid'],$data['token']);
    //     if($checktoken==1){
    //         // 100期内杀手数据
    //         for($i=1;$i<9;$i++){
    //             $list[$i-1]['smallroomid']=$i;
    //             $list[$i-1]['smallroomname']=Common::smallroomname($i);
    //             $maxid = Db::table("wa_killer_record")->max('id');
    //             $startid = $maxid-100;
    //             $list[$i-1]['count']=Db::table("wa_killer_record")->orderByDesc('id',true)->limit(100)->where('samllroomid',$i)->where("id",">",$startid)->count();
    //             // $new[]=$list;
    //         }
    //         // 近10期杀手房间记录
    //         $killer = Db::table("wa_killer_record")->orderByDesc('id',false)->limit(10)->get();

    //         // 我的投注记录
    //         $myinfo = Common::getmytzinfo($data['uid']);
    //         // 根据房间号获取 销毁 能量池  昨天今天数量
    //         $rs['jiangchi'] = Common::getjiangchirecord($data['roomid']);

    //         $rs['list']=$list;
    //         $rs['killer']=$killer;
    //         $rs['myrecord']=$myinfo;
    //         $tz=[
    //             'code'=>1,
    //             'msg'=>'获取成功',
    //             'info'=>$rs
    //         ];
    //         return $tz;
    //     }
    // }

    // 获取房间信息
    public static function getroominfo(){
        $room = Db::table("wa_rooms")->get()->map(function ($value) {return (array)$value;})
        ->toArray();
  
        foreach ($room as $k =>$v){
            $v['tzlist']=self::handletzlista($v['tzlist']);
            $list[] =$v;
        }
        $tz=[
            'code'=>1,
            'msg'=>'获取成功',
            'info'=>$list
        ];

        return $tz;
    }


    public static function handletzlista($data){
        $list = explode(",",$data);

        return $list;
    } 

}